In the Java Printing library, the LinePrinter class represents a mobile receipt printer and provides methods to communicate with the printer. To print a receipt, the application needs to perform the following steps:
Android OS displays the "Application Not Responding" (ANR) message if the application performs a lengthy operation (about 5 seconds) on the UI thread. While this occurs, the system cannot process incoming user input events. The printing API communicates with the printer through a network connection, which may invoke unpredictable delays that result in the ANR message.
To prevent the ANR message, the LinePrinter constructors and methods must be called on a separate thread on the Android platform. The only exceptions are the methods that add or remove an event listener. Most LinePrinter methods throw a LinePrinterException if called from the UI thread.
This code snippet demonstrates the steps described in the previous section. The Honeywell Mobility Android Printing SDK also includes a sample application called LinePrinterSample.
import com.honeywell.mobility.print.*;
import android.os.AsyncTask;
import android.app.Activity;
public class PrintActivity extends Activity {
private void createPrintTask()
{
PrintTask task = new PrintTask();
// Executes PrintTask with the specified parameters which will
// be passed to the PrintTask.doInBackground method. In this
// case, the doInBackgroud method does not expect any parameter.
task.execute();
}
class PrintTask extends AsyncTask<Void, Integer, Void>
{
/**
* This method runs on a background thread. The specified parameters
* are the parameters passed to the execute method by the caller of
* this task. This method can call publishProgress to publish updates
* on the UI thread.
*/
@Override
protected Void doInBackground(Void... args)
{
doPrint();
return null;
}
/**
* Runs on the UI thread after publishProgress is invoked. The
* specified values are the values passed to publishProgress.
*/
@Override
protected void onProgressUpdate(Integer... values)
{
// Access the values array.
int progress = values[0];
switch (progress)
{
case PrintProgressEvent.MessageTypes.CANCEL:
// You may display "Printing cancelled".
break;
case PrintProgressEvent.MessageTypes.COMPLETE:
// You may display "Printing completed".
break;
case PrintProgressEvent.MessageTypes.ENDDOC:
// You may display "End of document".
break;
case PrintProgressEvent.MessageTypes.FINISHED:
// You may display "Printer connection closed".
break;
case PrintProgressEvent.MessageTypes.STARTDOC:
// You may display "Start printing document".
break;
default:
// You may display "Unknown progress message" or do nothing.
break;
}
}
private void doPrint()
{
LinePrinter.ExtraSettings exSettings = new LinePrinter.ExtraSettings();
exSettings.setContext(PrintActivity.this);
try
{
// Creates a LinePrinter object with the specified
// parameters. The URI string "bt://00:02:5B:00:02:78"
// specifies to connect to the printer via Bluetooth
// and the Bluetooth MAC address.
File profiles = new File (getExternalFilesDir(null), "printer_profiles.JSON");
LinePrinter lp = new LinePrinter(profiles.getAbsolutePath(), "PR2",
"bt://00:02:5B:00:02:78", exSettings);
lp.addPrintProgressListener(new PrintProgressListener() {
public void receivedStatus(PrintProgressEvent aEvent)
{
// Publishes updates on the UI thread.
publishProgress(aEvent.getMessageType());
}
});
lp.connect(); // Connects to the printer
lp.setBold(true); // Sets bold font.
lp.write("SALES ORDER");
lp.setBold(false); // Returns to normal font.
lp.newLine(2);
lp.write(" PRD. DESCRIPT. PRC. QTY. NET.");
lp.newLine(2);
lp.writeLine(" 1501 Timer-Md1 13.15 1 13.15");
lp.writeLine(" 1502 Timer-Md2 13.15 3 39.45");
lp.writeLine(" 1503 Timer-Md3 13.15 1 13.15");
lp.writeLine(" 1504 Timer-Md4 13.15 4 52.60");
lp.writeLine(" 1505 Timer-Md5 13.15 5 65.75");
lp.writeLine(" ---- ------");
lp.write(" SUBTOTAL 15 197.25");
lp.newLine(2);
lp.writeLine(" ---- ------");
lp.write(" TOTAL SALES 15 197.15");
lp.newLine(4);
}
catch (LinePrinterException ex)
{
// Handles LinePrinter exceptions
}
catch (Exception ex)
{
// Handles other exceptions
}
finally
{
if (lp != null)
{
try
{
lp.disconnect(); // Disconnects from the printer
lp.close(); // Releases resources
}
catch (Exception ex) {}
}
}
}
}
}